package cn.bdqn.controller;

import cn.bdqn.domain.Admin;
import cn.bdqn.domain.BaseRegister;
import cn.bdqn.service.AdminService;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理员类控制层
 *
 * @Author 周安康
 * @create 2020/8/21 17:13
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    /**
     * 管理员类业务接口
     */
    @Autowired
    private AdminService adminService;

    /**
     * 根据id逻辑删除管理员信息
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public Result deleteById(Integer id) throws Exception{
        try{
            adminService.deleteByPrimaryKey(id);
            //返回结果
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }

    }

    /**
     * 添加管理员
     * @param record
     * @return
     */
    @RequestMapping("/add")
    public Result addAdmin(Admin record) throws Exception {
        try {
           adminService.insert(record);
           //返回结果
           return  ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 添加
     * @param record
     * @return
     * @throws Exception
     */
    @RequestMapping("/addSelective")
    public Result addSelective(Admin record)throws Exception {
        try {
            adminService.insertSelective(record);
            //返回结果
            return  ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectById")
    public Result selectByPrimaryKey(Integer id)throws Exception {
        try{
            Admin admin = adminService.queryByPrimaryKey(id);
            //返回结果
            return ResultGenerator.genSuccessResult(admin);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 选择更新
     * @param record
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateSelective")
    public Result updateByPrimaryKeySelective(Admin record)throws Exception{
        try{
            adminService.updateByPrimaryKeySelective(record);
            //返回结果
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 更新全部
     * @param record
     * @return
     * @throws Exception
     */
    @RequestMapping("/update")
    public Result updateByPrimaryKey(Admin record)throws Exception {
        try{
            adminService.updateByPrimaryKey(record);
            //返回结果
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 登录
     * @param phone
     * @param pwd
     * @return
     * @throws Exception
     */
    @RequestMapping("/login")
    public Result login(String phone, String pwd)throws Exception {
        try{
            Admin admin = adminService.queryByPhoneAndPwd(phone,pwd);

            if (admin!=null){
                //返回结果
                return ResultGenerator.genSuccessResult(admin);
            }
            //返回结果
            return ResultGenerator.genFailResult("账号或密码错误");

        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 判断手机号是否存在
     * @param phone 手机号
     * @return 返回结果   true 存在    false不存在
     * @throws Exception
     */
    @RequestMapping("/queryPhone")
    public Result queryByPhone(String phone)throws Exception{
        try{
            Boolean result = adminService.queryByPhone(phone);
            //返回结果
            return ResultGenerator.genSuccessResult(result);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 判断邮箱是否存在
     * @param email
     * @return 返回结果   true 存在    false不存在
     * @throws Exception
     */
    @RequestMapping("/queryEmail")
    public Result queryByEmail(String email)throws Exception{
        try{
            Boolean result = adminService.queryByEmail(email);
            //返回结果
            return ResultGenerator.genSuccessResult(result);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 根据旧密码和id修改基地账号数据
     * @param id
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @RequestMapping("/updateByIdAndOldPwd")
    public Result updateByIdAndOldPwd(Integer id, String oldPwd, String newPwd) throws Exception {
        try {
            Admin admin = adminService.updateByIdAndOldPwd(id,oldPwd,newPwd);
            //判断对象是否为空 为空代表修改失败
            if (admin==null){
                return ResultGenerator.genFailResult("旧密码输入错误");
            }else {
                //修改成功,返回最新的数据对象
                return ResultGenerator.genSuccessResult(admin);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception(e.getMessage());
        }
    }

}
